import 'package:flutter/material.dart';

class IconPicker extends StatefulWidget {
  IconPicker({@required this.currentIconData, @required this.onIconChanged});
  final ValueChanged<String> onIconChanged;
  final String currentIconData;
  final List<String> icons = [
    'default',
    '12306',
    'alipay',
    'dingding',
    'facebook',
    'github',
    'google',
    'ins',
    'ios',
    'qq',
    'wangyi',
    'wangyiyun',
    'wechat',
    'weibo',
    'xiaomi',
  ];

  @override
  State<StatefulWidget> createState() => _IconPickerState();
}

class _IconPickerState extends State<IconPicker> {
  String selectedIconData;

  @override
  void initState() {
    selectedIconData = widget.currentIconData;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Orientation orientation = MediaQuery.of(context).orientation;
    return Container(
      width: orientation == Orientation.portrait ? 300 : 300,
      height: orientation == Orientation.portrait ? 360 : 200,
      child: GridView.builder(
        itemBuilder: (BuildContext context, int index) {
          var iconData = widget.icons[index];
          return Material(
            color: Colors.transparent,
            child: InkWell(
              onTap: () {
                setState(() {
                  selectedIconData = iconData;
                });
                widget.onIconChanged(iconData);
              },
              borderRadius: BorderRadius.circular(5),
              child: Center(
                child: Image.asset('assets/images/$iconData.png'),
              ),
            ),
          );
        },
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: orientation == Orientation.portrait ? 4 : 6,
          mainAxisSpacing: 20,
          crossAxisSpacing: 20,
          childAspectRatio: 1,
        ),
        itemCount: widget.icons.length,
      ),
    );
  }
}
